﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Helpers;

namespace LifeFlow.Servicer
{
    public class ServiceParameter
    {
        private static readonly string[] baseTypes = new[] { "string", "int", "double", "bool", "float", "byte" };
        public static T GetParameter<T>(string name, bool post = false)
        {
            string value = null;
            value = !post ? HttpContext.Current.Request.QueryString[name] : HttpContext.Current.Request.Params[name];

            if (value == null)
                return default(T);
            if (!baseTypes.Contains(typeof(T).Name.ToLower()))
                return Json.Decode<T>(value);
            return (T) Convert.ChangeType(value, typeof (T));
        }

        public static object GetParameter(string name, bool post = false)
        {
            string value = null;
            value = !post ? HttpContext.Current.Request.QueryString[name] : HttpContext.Current.Request.Params[name];

            if (value == null)
                return null;
            return Json.Decode(value);
        }
    }
}
